package com.zt.questionnaire.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * 参考文档：https://blog.csdn.net/A169388842/article/details/84989796。
 *
 * 加载本类：
 * spring.factories(在src/main/resources建立文件夹META-INF)
	org.springframework.boot.env.PropertySourceLoader=\
	com.springcloud.config.server.env.PropertiesPropertySourceLoader
 *
 * 重写PropertiesPropertySourceLoader，解决中文乱码的问题。
 * 乱码的原因是：spring 默认使用org.springframework.boot.env.PropertiesPropertySourceLoader 来加载配置，底层是通过调用 Properties 的 load 方法，而load方法输入流的编码是 ISO 8859-1。
 * 实现org.springframework.boot.env.PropertySourceLoader 接口，重写 load 方法。
 * properties property source loader and this class load properties file encoding is UTF-8
 *
 * @see org.springframework.boot.env.PropertiesPropertySourceLoader
 **/
@Slf4j
public class PropertiesPropertySourceLoader implements PropertySourceLoader {


    private static final String CHARSET_NAME = "utf-8";

    private static final String XML_FILE_EXTENSION = ".xml";

    @Override
    public String[] getFileExtensions() {
        return new String[]{"properties", "xml"};
    }

//    @Override
//	public List<PropertySource<?>> load(String name, Resource resource)
//			throws IOException {
//		Map<String, ?> properties = loadProperties(resource);
//		if (properties.isEmpty()) {
//			return Collections.emptyList();
//		}
//		return Collections
//				.singletonList(new OriginTrackedMapPropertySource(name, properties));
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	private Map<String, ?> loadProperties(Resource resource) throws IOException {
//		String filename = resource.getFilename();
//		if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
//			return (Map) PropertiesLoaderUtils.loadProperties(resource);
//		}
//		return new OriginTrackedPropertiesLoader(resource).load();
//	}


    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {

        Properties properties = getProperties(resource);
        if (properties.isEmpty()) {
			return Collections.emptyList();
		}
        PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(name, properties);
//        return propertiesPropertySource;
        return Collections.singletonList(propertiesPropertySource);

    }

    private Properties getProperties(Resource resource) {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
            properties.load(new InputStreamReader(inputStream, CHARSET_NAME));
            log.info("properties: {}", properties);
            inputStream.close();
        } catch (IOException e) {
            log.error("load inputstream failure: {}", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("close IO failure: {}", e);
                }
            }
        }
        return properties;
    }


}


